using System;
using System.Collections;
using UnityEngine;
using UnityEngine.Serialization;

public class Player : MonoBehaviour
{
    public float hp;
    public float maxHp;
    public float mp;
    public float maxMp;
    public float arm;
    public float maxArm;
    public float armCDTimer = 0;
    public float armCD = 3f;
    public bool armCDState = true;
    public bool armImmuneState = false;

    private Animator animator;

    private void Awake()
    {
        animator = GetComponent<Animator>();
    }

    private void FixedUpdate() //默认0.02s，可在Project setting -> time 设置
    {
        armCDTimer += Time.fixedDeltaTime;
        if (armCDTimer >= armCD)
        {
            armCDState = true;
        }
    }

    private void Update()
    {
        if (armCDState && armCDTimer >= 1)
        {
            arm = Math.Min(arm + 1, maxArm);
            armCDTimer = 0;
        }
    }

    public IEnumerator Die()
    {
        yield return new WaitForSeconds(1f);
        GameController.Instance().panelCtr.LoadGameOverPanel();
    }

    public void BeAttacked()
    {
        armCDState = false;
        armCDTimer = 0;
        animator.Play("BeAttacked");

        GameController.Instance().panelCtr.playerPanel.UpdateAllValue();
        
        if (hp <= 0)
        {
            GameController.Instance().FSM.CurPlayerState = PlayerState.Die;
            animator.SetBool("die", true);
            GetComponent<Collider2D>().enabled = false;
            StartCoroutine(Die());
        }
    }

    public void SetHP()
    {
        this.hp = Math.Min(hp, maxHp);
    }
    
    public void SetHP(int value)
    {
        this.hp = Math.Min(hp + value, maxHp);
    }
    
    public void SetMP(int value)
    {
        this.mp = Math.Min(mp + value, maxMp);
    }
}